package com.fluo;

import java.io.IOException;

import android.app.ListActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;

public class FluoPlayer extends ListActivity {
	
	private MediaPlayer mp = new MediaPlayer();
	
	private ImageButton pause;
	private ImageButton prevBtn;
	private ImageButton nextBtn;
	private ProgressBar progress;
	private int listPosition;
    
	
	    /** Called when the activity is first created. */
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.fluoplayer);
	        
	    	progress = (ProgressBar) findViewById(R.id.progress);
	    	       	
	        updateSongList();
	        
	        ProgressBarUpdate barUpdate = new ProgressBarUpdate(progress,mp);
	    }

	    public void updateSongList() {
	    	ArrayAdapter<String> songList = new ArrayAdapter<String>(this,R.layout.song_item,R.id.line1,ServerConnect.playList);
	    	setListAdapter(songList);  	
	    }
 
@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
	
	////////////////////
	// Play/Pause Button
	////////////////////
	pause = (ImageButton) findViewById(R.id.pause);
	pause.setOnClickListener(new View.OnClickListener() {
		public void onClick(View view) {
			if (mp.isPlaying()) 
			{
				mp.pause();
				progress.setProgress(mp.getCurrentPosition());
				//pause.setImageResource(android.R.style.MediaButton_Play );
			} 
			else 
			{
				mp.start();
				//pause.setImageResource(android.R.style.MediaButton_Pause);
			}
    }});
	
	////////////////////
	// Previous Button
	////////////////////
	prevBtn = (ImageButton) findViewById(R.id.prev);
	prevBtn.setOnClickListener(new View.OnClickListener() {
		public void onClick(View view) {
			// Checks if listPosition is at the start of the playlist
			if(listPosition > 0)
			{
				listPosition = listPosition - 1;
				mp.reset();
				try {
					mp.setDataSource(ServerConnect.playList.get(listPosition));
					mp.prepare();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				mp.start();
				progress.setMax(mp.getDuration());
			}
			// If at the first song, it will play the last song of the play list
			else
			{
				listPosition = ServerConnect.playList.size();
				mp.reset();
				try {
					mp.setDataSource(ServerConnect.playList.get(listPosition));
					mp.prepare();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				mp.start();
				progress.setMax(mp.getDuration());
			}
    }});
	
	////////////////////
	// Next Button
	////////////////////
	nextBtn = (ImageButton) findViewById(R.id.next);
	nextBtn.setOnClickListener(new View.OnClickListener() {
		public void onClick(View view) {
			// Checks if listPosition is at the end of the playlist
			if(listPosition < (ServerConnect.playList.size() - 1))
			{
				listPosition = listPosition + 1;
				mp.reset();
				try {
					mp.setDataSource(ServerConnect.playList.get(listPosition));
					mp.prepare();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				mp.start();
				progress.setMax(mp.getDuration());
			}
			// If it is at the end, then the next song is the first on the list
			else
			{
				listPosition = 0;
				mp.reset();
				try {
					mp.setDataSource(ServerConnect.playList.get(listPosition));
					mp.prepare();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				mp.start();
				progress.setMax(mp.getDuration());
			}
    }});
    	
	
	////////////////////
	// List Click
	////////////////////
	mp.reset();
	try {
		mp.setDataSource(ServerConnect.playList.get(position));
		listPosition = position;
		mp.prepare();
	} catch (IllegalStateException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	mp.start();
	progress.setMax(mp.getDuration());
	}
}


class ProgressBarUpdate implements Runnable
{
	ProgressBar progress;
	MediaPlayer mp;
	public ProgressBarUpdate(ProgressBar progressBar, MediaPlayer mPlayer){
		progress = progressBar;
		mp = mPlayer;
		Thread t = new Thread(this);
		t.start();
	}
	public void run(){
		while(true){
			progress.setProgress(mp.getCurrentPosition());
		}
	}
}
